package it.unibo.convoycruisecontrol.chiefboatsubsystem.test.unit;

import static org.junit.Assert.*;
import it.unibo.convoycruisecontrol.boatsubsystem.test.mock.MockLocation;
import it.unibo.convoycruisecontrol.boatsubsystem.test.mock.MockLocationDevice;
import it.unibo.convoycruisecontrol.chiefboatsubsystem.MapManager;
import it.unibo.convoycruisecontrol.chiefboatsubsystem.StartSupervisor;
import it.unibo.convoycruisecontrol.chiefboatsubsystem.test.mock.MockStartSupervisor;
import it.unibo.location.ILocation;
import it.unibo.util.information.PrologInformationConverter;

import org.junit.BeforeClass;
import org.junit.Test;

public class StartSupervisorTestCase 
{
	static MockStartSupervisor startSupervisor;
	static int startDistance = 150;
	static int boatsNumber = 3;

	@BeforeClass
	public static void beforeClass()
	{
		try
		{
			startSupervisor = new MockStartSupervisor(startDistance, boatsNumber, new MockLocationDevice(), new PrologInformationConverter());
		}
		catch (Exception e) {
			e.printStackTrace();
			fail();
		}
	}

	@Test
	public void testStart() 
	{
		try
		{
			boolean[] expected = new boolean[]{false, false, false};
			for(int i=0; i < expected.length; i++)
			{
				assertEquals(expected[i], startSupervisor.getBoatsStarted()[i]);
			}

			// Faccio partire la prima barca
			startSupervisor.startBoat(0);
			expected = new boolean[]{true, false, false};
			for(int i=0; i < expected.length; i++)
			{
				assertEquals(expected[i], startSupervisor.getBoatsStarted()[i]);
			}

			// 200m - 100m - -> parte la seconda
			ILocation locations[] = new ILocation[boatsNumber];	
			locations[0] = new MockLocation(40.67, 15.9, 0);
			locations[1] = new MockLocation(40.67, 15.9027, 0);
			locations[2] = new MockLocation(40.67, 15.9036, 0);
			startSupervisor.setLocations(locations);
			startSupervisor.newInformations();
			startSupervisor.setLocations(locations);		// Giusto per fare vedere che non partono altre barche se arrivano le stesse info
			startSupervisor.newInformations();
			expected = new boolean[]{true, true, false};
			for(int i=0; i < expected.length; i++)
			{
				assertEquals(expected[i], startSupervisor.getBoatsStarted()[i]);
			}

			// 200m - 200m - -> parte la terza
			locations[0] = new MockLocation(40.67, 15.9, 0);
			locations[1] = new MockLocation(40.67, 15.9027, 0);
			locations[2] = new MockLocation(40.67, 15.9054, 0);
			startSupervisor.setLocations(locations);
			startSupervisor.newInformations();
			startSupervisor.setLocations(locations);		// Giusto per fare vedere che non partono altre barche se arrivano le stesse info
			startSupervisor.newInformations();
			expected = new boolean[]{true, true, true};
			for(int i=0; i < expected.length; i++)
			{
				assertEquals(expected[i], startSupervisor.getBoatsStarted()[i]);
			}
			
			assertTrue(startSupervisor.isTerminated());
		}
		catch (Exception e) {
			e.printStackTrace();
			fail();
		}
	}
	
	@Test(expected=IllegalArgumentException.class)
	public void testCreation() throws Exception
	{
		new StartSupervisor(9,null,null);
	}

}
